﻿namespace AppLab
{
    using System;
    using System.Linq;
    using System.Windows.Data;
    using AppLab.Web;
    using System.Windows;


    public class BrandValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return new Brand() { BrandId = (int)value };
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var brand = value as Brand;
            if (brand == null)
                return null;
            return brand.BrandId;
        }
    }

    public class SubmitTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return new SubmitType() { SubmitTypeId = (int)value };
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var submittype = value as SubmitType;
            if (submittype == null)
                return null;
            return submittype.SubmitTypeId;
        }
    }

    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value,
                   Type targetType,
                   object parameter,
                   System.Globalization.CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            return date.ToShortDateString();
        }

        public object ConvertBack(object value,
           Type targetType,
           object parameter,
           System.Globalization.CultureInfo culture)
        {
            string strValue = value.ToString();
            DateTime resultDateTime;
            if (DateTime.TryParse(strValue, out resultDateTime))
            {
                return resultDateTime;
            }
            return value;

        }

    }

    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool i = (bool)value;
            if (i)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility v = (Visibility)value;
            if (v == Visibility.Collapsed)
                return false;
            else
                return true;
        }
    }


    public class MobileTypeValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return new MobileType() { MobileId = (int)value };
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var mobile = value as MobileType;
            if (mobile == null)
                return null;
            return mobile.MobileId;
        }
    }
}
